#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<stack>

using namespace std;

int minLength(string s) {
    stack<char> stk;
    for (auto c : s)
    {
        if (!stk.empty() && (c == 'B' || c == 'D'))
        {
            char t = stk.top();
            if ((t == 'A' && c == 'B') || (t == 'C' && c == 'D'))
                stk.pop();
            else
                stk.push(c);
        }
        else    stk.push(c);
    }
    return stk.size();
}

int main()
{
    minLength("D");
    return 0;
}